package com.baizhi.service.impl;

import com.baizhi.entity.BzMenu;
import com.baizhi.mapper.BzMenuMapper;
import com.baizhi.service.BzMenuService;
import com.baizhi.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author yu
 */
@Service
public class BzMenuServiceImpl implements BzMenuService {
    @Autowired
    private BzMenuMapper bzMenuMapper;
    @Override
    public List<MenuVo> showMenuList() {
        /*List<MenuVo> menuVos = bzMenuMapper.showMenuList();
        return menuVos;*/

        List<BzMenu> bzMenus = bzMenuMapper.selectList(null);
        List<MenuVo> menuVos = showTree(0, bzMenus);
        return menuVos;
    }
//    用流完成Tree
    public List<MenuVo> showTree(long parentId,List<BzMenu> bzMenus){
        List<MenuVo> list = bzMenus.stream().filter(new Predicate<BzMenu>() {
            @Override
            public boolean test(BzMenu bzMenu) {
                return bzMenu.getParentId() == parentId;
            }
        }).map(new Function<BzMenu, MenuVo>() {
            @Override
            public MenuVo apply(BzMenu bzMenu) {
                MenuVo menuVo = new MenuVo();
                menuVo.setMenuId(bzMenu.getMenuId());
                menuVo.setName(bzMenu.getName());
                menuVo.setUrl(bzMenu.getUrl());
                menuVo.setMenuVos(showTree(bzMenu.getMenuId(), bzMenus));
                return menuVo;
            }
        }).collect(Collectors.toList());
        return list;
    }
}
